#cmake file for project facecl
#author:guyadong
#created:2016/01/09
# 版本号
set (FACECL_VERSION_MAJOR 1)
set (FACECL_VERSION_MINOR 0)
#项目编译要求的OpenCL版本号
set (OPENCL_VERSION_REQUIRED "1.2" CACHE STRING "OpenCl version required")
set_property(CACHE OPENCL_VERSION_REQUIRED PROPERTY STRINGS "1.0" "1.1" "1.2" "2.0" "2.1")

############################################################################
cmake_minimum_required( VERSION 2.6 )
project( facecl)
#目标处理器
if(NOT DEFINED TARGET_PROCESSOR)
	set( TARGET_PROCESSOR ${CMAKE_SYSTEM_PROCESSOR} CACHE STRING  "target processor")
endif()
set_property(CACHE TARGET_PROCESSOR PROPERTY STRINGS x86 AMD64 )

if(NOT CMAKE_BUILD_TYPE )
  set(CMAKE_BUILD_TYPE RELEASE)
endif()

message(STATUS "Project: ${PROJECT_NAME}") 
message(STATUS "CMAKE_BUILD_TYPE= ${CMAKE_BUILD_TYPE}")
message(STATUS "CMAKE_SYSTEM_NAME: ${CMAKE_SYSTEM_NAME}")  
#判断编译类型和版本是否满足编译要求
if(MSVC AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 19)
	message(STATUS "CMAKE_CXX_COMPILER: ${CMAKE_CXX_COMPILER}") 
	message(STATUS "CMAKE_CXX_COMPILER_VERSION=${CMAKE_CXX_COMPILER_VERSION}" )
elseif(CMAKE_COMPILER_IS_GNUCXX AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 5)
	message(STATUS "CMAKE_CXX_COMPILER: ${CMAKE_CXX_COMPILER}") 
	message(STATUS "CMAKE_CXX_COMPILER_VERSION=${CMAKE_CXX_COMPILER_VERSION}" )
else()
	message(FATAL_ERROR "compiler required: Visual Studio 2015 OR gcc 5.2.0" )
endif()
if(TARGET_PROCESSOR)
	set (CMAKE_SYSTEM_PROCESSOR ${TARGET_PROCESSOR})
endif()
message(STATUS "CMAKE_SYSTEM_PROCESSOR: ${CMAKE_SYSTEM_PROCESSOR}")
##############定义安装位置##############
if(CMAKE_SYSTEM_PROCESSOR MATCHES "AMD64")
	set (target_cpu x86_64)
else()
	string(TOLOWER ${CMAKE_SYSTEM_PROCESSOR} target_cpu)
endif()
string(TOLOWER ${CMAKE_BUILD_TYPE}/detect_cl_${CMAKE_SYSTEM_NAME}_${CMAKE_CXX_COMPILER_ID}_${target_cpu} release_folder)
set(CMAKE_INSTALL_PREFIX ${CMAKE_SOURCE_DIR}/${release_folder} CACHE PATH
	"folder to install detect_cl"
	FORCE)
MESSAGE( STATUS "CMAKE_INSTALL_PREFIX =${CMAKE_INSTALL_PREFIX}" )
#定义公共代码位置   
set( COMMONS_SOURCE_FOLDER ${CMAKE_SOURCE_DIR}/../common_source_cpp CACHE STRING "common source folder" FORCE)
if( NOT IS_DIRECTORY "${COMMONS_SOURCE_FOLDER}" )
	MESSAGE( FATAL_ERROR "Found common source folder: ${COMMONS_SOURCE_FOLDER}, please locate it" )	
endif()
MESSAGE( STATUS "COMMONS_SOURCE_FOLDER= ${COMMONS_SOURCE_FOLDER}" )
set( SUBDIRECTORIES opencl zlib libb64 jsoncpp faceimage facedetect )
#判断编译器类型,如果是gcc编译器,则在编译选项中加入c++11支持
if(CMAKE_COMPILER_IS_GNUCXX)
	set(CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS}")
	message(STATUS "optional:-std=c++11")
endif(CMAKE_COMPILER_IS_GNUCXX)
if(MSVC)     
	#关闭C4819警告
	add_definitions("/wd4819")		
	message(STATUS "optional:/wd4819")
	#关闭CRT_SECURE警告
	add_definitions(-D_CRT_SECURE_NO_WARNINGS)
	message(STATUS "optional:-D_CRT_SECURE_NO_WARNINGS")	
    # Use the static C library for all build types
    foreach(var 
    	CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE
      	CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO
      	CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
      	CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO
      )
      if(${var} MATCHES "/MD")
        string(REGEX REPLACE "/MD" "/MT" ${var} "${${var}}")
      endif()
    endforeach()	
endif(MSVC)
set (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set (CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
foreach( subdir ${SUBDIRECTORIES} )
	if( IS_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/${subdir}" )
		MESSAGE( STATUS "Found sub project ${subdir}, adding it" )
		add_subdirectory( ${subdir} )
	endif()
endforeach()
if(TARGET_PROCESSOR STREQUAL  "x86" AND MSVC)  
	MESSAGE( STATUS "TARGET_PROCESSOR is ${TARGET_PROCESSOR},so adding sub dir \"facesdktest\"" )	
	add_subdirectory( facesdk.x86 )
	add_subdirectory( facesdktest )
endif()

set(CMAKE_GNUtoMS ON )